﻿using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WCSFContrib.ECommerceCatalog.User.Views;
using WCSFContrib.ECommerceCatalog.User.Tests.Mocks;
using WCSFContrib.ECommerceCatalog.BusinessEntities;

namespace WCSFContrib.ECommerceCatalog.User.Tests
{
    /// <summary>
    /// Summary description for CheckoutPresenterTestFixture
    /// </summary>
    [TestClass]
    public class CheckoutPresenterTestFixture
    {
        private MockCheckoutView _view;
        private MockUserController _controller;

        public CheckoutPresenterTestFixture()
        {
        }

        private CheckoutPresenter GetCheckoutPresenterInitialized()
        {
            _controller = new MockUserController();
            _view = new MockCheckoutView();
            CheckoutPresenter presenter = new CheckoutPresenter(_controller);
            presenter.View = _view;

            return presenter;
        }

        [TestMethod]
        public void ShouldShowOrdersOnViewInitializedWhenCartIsNotNull()
        {
            CheckoutPresenter presenter = GetCheckoutPresenterInitialized();

            presenter.UserName = "userTest";
            presenter.OnViewInitialized();

            Assert.IsTrue(_controller.GetUserProfileCalled);
            Assert.IsTrue(_view.CurrentUserCalled);
            Assert.IsTrue(_view.OrderDateCalled);
            Assert.IsTrue(_view.AddressListCalled);
            Assert.IsTrue(_view.PaymentsMethodsListCalled);

            Assert.AreEqual(2, _view.AddressListCount);
            Assert.AreEqual(1, _view.PaymentsMethodsListCount);
        }

        [TestMethod]
        public void ShouldShowOrdersOnViewInitializedWhenCartIsNull()
        {
            CheckoutPresenter presenter = GetCheckoutPresenterInitialized();

            presenter.UserName = "unknownUser";
            presenter.OnViewInitialized();

            Assert.IsTrue(_controller.GetUserProfileCalled);
            Assert.IsFalse(_view.CurrentUserCalled);
            Assert.IsFalse(_view.OrderDateCalled);
            Assert.IsFalse(_view.AddressListCalled);
            Assert.IsFalse(_view.PaymentsMethodsListCalled);

            Assert.AreEqual(0, _view.AddressListCount);
            Assert.AreEqual(0, _view.PaymentsMethodsListCount);
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion
    }

    class MockCheckoutView : ICheckoutView
    {
        public bool CurrentUserCalled = false;
        public bool OrderDateCalled = false;
        public bool AddressListCalled = false;
        public bool PaymentsMethodsListCalled = false;
        public int AddressListCount;
        public int PaymentsMethodsListCount;

        private ICollection<Address> _addressList;
        private ICollection<Payment> _paymentsMethodsList;
        private string _currentUser = string.Empty;
        private DateTime _orderDate = DateTime.Now;

        #region ICheckoutView Members

        public string CurrentUser
        {
            get
            {
                return _currentUser;
            }
            set
            {
                CurrentUserCalled = true;
                _currentUser = value;
            }
        }

        public DateTime OrderDate
        {
            get
            {
                return _orderDate;
            }
            set
            {
                OrderDateCalled = true;
                _orderDate = value;
            }
        }

        public ICollection<Address> AddressList
        {
            set 
            {
                AddressListCalled = true;
                _addressList = value;
                AddressListCount = _addressList.Count;
            }
        }

        public ICollection<Payment> PaymentsMethodsList
        {
            set 
            {
                PaymentsMethodsListCalled = true;
                _paymentsMethodsList = value;
                PaymentsMethodsListCount = _paymentsMethodsList.Count;
            }
        }

        public int BillingAddressId
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public int ShippingAddressId
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public int PaymentMethodId
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        #endregion
    }
}


